#include "Level.hpp"

#include <iostream>
#include <fstream>

int Level::getTile(int floor, int angle)const
{
    if ( floor < 0 || floor > 6 )
    {
        std::cerr << "Level::getTile -> Invalid level '" << floor << "'" << std::endl;
        return -1;
    }

    if ( angle < 0 || angle > 360 )
    {
        std::cerr << "Level::getTile -> Invalid angle '" << angle << "'" << std::endl;
        return -1;
    }

    return m_tiles[floor][angle];
}

void Level::setTile(int level, int angle, int id)
{
    if ( level < 0 || level > 6 )
    {
        std::cerr << "Level::setTile -> Invalid level '" << level << "'" << std::endl;
    }

    if ( angle < 0 || angle > 360 )
    {
        std::cerr << "Level::setTile -> Invalid angle '" << angle << "'" << std::endl;
    }

    // angle /= (360.0f / static_cast<float>(m_tiles[level].size()));

    m_tiles[level][angle] = id;
}

Level::TileType Level::getType(int id)const
{
    if ( id == 0 || id >= 1000 )
    {
        return TT_Hole;
    }
    else if ( id >= 1 && id < 100 )
    {
        return TT_Floor;
    }
    else if ( id >= 100 && id < 200 )
    {
        return TT_Wall;
    }
    else if ( id >= 200 && id < 300 )
    {
        return TT_TopWall;
    }

    std::cerr << "Level -> Invalid ID '" << id << "'" << std::endl;
    return TT_Hole;
}

bool Level::isHole(int id)const
{
    return getType(id) == TT_Hole;
}

bool Level::isFloor(int id)const
{
    return getType(id) == TT_Floor;
}

bool Level::isWall(int id)const
{
    return getType(id) == TT_Wall;
}

bool Level::isTopWall(int id)const
{
    return getType(id) == TT_TopWall;
}

void Level::print()const
{
    for(unsigned int i=0; i< m_tiles.size();i++) // vecteur de floors
    {
        std::cout << "Floor" << i << " : " << std::endl;
        for(unsigned int j=0; j<m_tiles[i].size();j++) //vecteur d'int
        {
            std::cout<< m_tiles[i][j] <<" ";
        }
        std::cout<<std::endl;
    }
}

void Level::save(const std::string& fileName)const
{
    std::ofstream levelFile(fileName.c_str());
    if ( !levelFile.is_open() )
    {
        std::cerr << "Fail to save the file in '" << fileName << "'" << std::endl;
        return;
    }

    for(unsigned int i=0; i< m_tiles.size();i++) // vecteur de floors
    {
        for(unsigned int j=0; j<m_tiles[i].size();j++) //vecteur d'int
        {
            levelFile << m_tiles[i][j] << " ";
        }
        levelFile << std::endl;
    }

    levelFile.close();
}
